package com.qbw.app.module.common.view;

import android.content.Context;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.widget.FrameLayout;

/**
 * @author QBW
 * @date 2019/11/21
 */
public class MakeParentNoScrollView extends FrameLayout {

    private ViewGroup mTargetViewGroup;

    public MakeParentNoScrollView(@NonNull Context context) {
        super(context);
    }

    public MakeParentNoScrollView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public MakeParentNoScrollView(@NonNull Context context,
                                  @Nullable AttributeSet attrs,
                                  int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (mTargetViewGroup != null) {
            int a = ev.getAction();
            //L.GL.e("" + ev.getAction());
            if (a == MotionEvent.ACTION_DOWN || a == MotionEvent.ACTION_POINTER_DOWN) {
                mTargetViewGroup.requestDisallowInterceptTouchEvent(true);
            } else if (a == MotionEvent.ACTION_MOVE || a == MotionEvent.ACTION_HOVER_MOVE) {
                //do nothing
            } else if (a == MotionEvent.ACTION_UP || a == MotionEvent.ACTION_CANCEL || a == MotionEvent.ACTION_OUTSIDE) {
                mTargetViewGroup.requestDisallowInterceptTouchEvent(false);
            }
        }
        return super.dispatchTouchEvent(ev);
    }

    public void setTargetViewGroup(ViewGroup targetViewGroup) {
        mTargetViewGroup = targetViewGroup;
    }
}
